/*
 *  This class is part of WorkTimeCalculator Android App
 *  Author: Domenico Pio Novelli
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.dpndeveloper.wtc.dataprovider;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import android.content.Context;
import android.database.Cursor;

/**
 * @author: Domenico Pio Novelli
 */
public class WtcIntervalDataBaseHelper extends BaseDatabaseHelper<WtcInterval> {

	public WtcIntervalDataBaseHelper(Context context) {
		super(context);
	}

	@Override
	public long insert(WtcInterval wtcInterval) {
		return executeInsertIntoTable(DbConstants.TABLE_TIME_INTERVAL,
				createValues(wtcInterval));
	}

	private Map<String, String> createValues(WtcInterval wtcInterval) {
		Map<String, String> values = new HashMap<String, String>();
		values.put(DbConstants.TABLE_TIME_INTERVAL_F_START_TIME, ""
				+ wtcInterval.getStartTime().getTimeInMillis());
		long end = 0;
		if (wtcInterval.getEndTime() != null) {
			end = wtcInterval.getEndTime().getTimeInMillis();
		}
		values.put(DbConstants.TABLE_TIME_INTERVAL_F_END_TIME, "" + end);
		values.put(DbConstants.TABLE_TIME_INTERVAL_F_DESCRIPTION,
				wtcInterval.getDescription());
		return values;
	}

	@Override
	public boolean update(WtcInterval wtcInterval) {
		return executeUpdateOnTable(DbConstants.TABLE_TIME_INTERVAL,
				createValues(wtcInterval), wtcInterval.getId().toString());
	}

	@Override
	public boolean delete(WtcInterval wtcInterval) {
		return executeDeleteOnTable(DbConstants.TABLE_TIME_INTERVAL,
				wtcInterval.getId().toString());

	}

	@Override
	public WtcInterval select(Integer id) {
		String[] params = { Integer.toString(id) };
		List<WtcInterval> result = executeSelect(
				DbConstants.TABLE_TIME_INTERVAL,
				DbConstants.TABLE_TIME_INTERVAL_COLUMNS, "id=?", params, null);
		if (result.size() == 1)
			return result.get(0);
		else
			return null;
	}

	@Override
	public List<WtcInterval> selectAll() {
		return executeSelect(DbConstants.TABLE_TIME_INTERVAL,
				DbConstants.TABLE_TIME_INTERVAL_COLUMNS, null, null,
				DbConstants.TABLE_TIME_INTERVAL_F_START_TIME
						+ " COLLATE NOCASE ASC");
	}

	@Override
	public List<WtcInterval> selectByParams(String selection, String[] params) {
		return executeSelect(DbConstants.TABLE_TIME_INTERVAL,
				DbConstants.TABLE_TIME_INTERVAL_COLUMNS, selection, params,
				DbConstants.TABLE_TIME_INTERVAL_F_START_TIME
						+ " COLLATE NOCASE ASC");
	}

	public List<WtcInterval> selectByInterval(Calendar startDate,
			Calendar endDate) {
		// Query to get WtcIntervals in a interval of time
		String selection = DbConstants.TABLE_TIME_INTERVAL_F_START_TIME
				+ ">=? AND " + DbConstants.TABLE_TIME_INTERVAL_F_START_TIME
				+ "<=?";

		String start = "" + startDate.getTimeInMillis();
		String end = "" + endDate.getTimeInMillis();

		// Perform a select of WtcIntervals in the current day
		String[] todayParams = new String[] { start, end };
		return selectByParams(selection, todayParams);

	}

	@Override
	protected WtcInterval extractResult(Cursor cursor) {
		WtcInterval wtcInterval = new WtcInterval();
		wtcInterval.setId(cursor.getInt(cursor
				.getColumnIndex(DbConstants.TABLE_TIME_INTERVAL_F_ID)));

		Calendar startTime = Calendar.getInstance(TimeZone.getDefault());
		startTime.setTimeInMillis(cursor.getLong(cursor
				.getColumnIndex(DbConstants.TABLE_TIME_INTERVAL_F_START_TIME)));
		wtcInterval.setStartTime(startTime);

		Calendar endTime = null;
		long end = cursor.getLong(cursor
				.getColumnIndex(DbConstants.TABLE_TIME_INTERVAL_F_END_TIME));
		if (end != 0) {
			endTime = Calendar.getInstance(TimeZone.getDefault());
			endTime.setTimeInMillis(end);
		}
		wtcInterval.setEndTime(endTime);

		wtcInterval
				.setDescription(cursor.getString(cursor
						.getColumnIndex(DbConstants.TABLE_TIME_INTERVAL_F_DESCRIPTION)));

		return wtcInterval;
	}

}